package com.cunjin.repair.service.impl;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.base.utils.ExportExcel;
import com.cunjin.repair.dao.RepairDao;
import com.cunjin.repair.entity.RepairEntity;
import com.cunjin.repair.service.RepairService;


@Service
public class RepairServiceImpl extends BaseServiceImpl<RepairEntity> implements RepairService {

	@Autowired
	private RepairDao repairDao;
	
	

	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName,String chuli) {
		
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", "%"+searchName+"%");
		}else if(null ==searchName){
			pages.put("searchName", null);
		}
		pages.put("chuli", chuli);
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<RepairEntity> ls=repairDao.myqueryByPage(pages);
			map.put("total",repairDao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> insert(RepairEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			List<RepairEntity> select = repairDao.select(entity);
			int size = select.size();
			//size==0说明没有个班级
			if(size==0){
				int count = repairDao.insert(entity);
				if(count==1){
					map.put("code", 200);
					map.put("msg","添加班级成功");
				}
				else{
					map.put("code", 500);
				}
			}else{
				map.put("code", 201);
				map.put("msg","班级已经存在");
			}
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				repairDao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("count", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	@Override
	public Map<String, Object> update(RepairEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//排除更新的宿舍重复性
		Integer oldId = entity.getId();
		String newname = entity.getDormitoryName();
		RepairEntity oldentity = repairDao.selectByPrimaryKey(oldId);
		String oldname = oldentity.getDormitoryName();
		//如果是两个相等没有修改宿舍名字
		if(oldname.equals(newname)){
			repairDao.updateByPrimaryKeySelective(entity);
			map.put("code", 200);
			map.put("msg","更新成功");
			return map;
		}else{
			map.put("code", 500);
			map.put("msg","没有这条记录");
			return map;
		}
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> exportExcel(String condition) {
		
		
		if("0".equals(condition)){
			condition="未处理";
		}else if("1".equals(condition)){
			condition="已处理";
		}else if("2".equals(condition)){
			condition=null;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		ExportExcel ex=new ExportExcel<RepairEntity>();
		String[] headers={"ID","保修时间","报修原因","宿舍名称","报修人","报修状态"};
		String title="维修单";
		List<RepairEntity> dataset=new ArrayList<RepairEntity>();
		dataset=repairDao.selectRepairBykeyword(condition);
		
		OutputStream out;
		try {
			out = new FileOutputStream("c://维修单.xls");
			
			String pattern="yyyy-MM-dd HH:mm:ss";
			ex.exportExcel(title,headers, dataset, out,pattern);
			out.close();
			map.put("code",200);
			map.put("msg", "导出成功");
		} catch (FileNotFoundException e) {
			map.put("code",201);
			map.put("msg", "文件正在使用");
			e.printStackTrace();
		} catch (IOException e) {
			map.put("code",500);
			map.put("msg", "导出失败");
			e.printStackTrace();
		}
		
		return map;
	}
}